/*
 Programmer : Liza Detrick
 Project : 9.3
 Class : CS 3A
 Due :  3/10/11
 Description : This is a program that assigns airplane seats 
 */
#include <iostream>
#include <cstdlib>

using namespace std;

void Get_input(int& rows, int& columns);//Gets input from the user
char** IntSeating(int& rows, int& columns);//Creates the airplane seating array 
void FillSeating(char **airplane, int& rows, int& columns);//Fills the seating array with letters
void Assign_seat(char **airplane, int seat, char& letter, int& rows, int& columns);//Allows the user to pick and assign a seat
void Convert_letter(char& letter); //Converts letter to a number
bool Check_Seat(char **airplane, int seat, char& letter);//Checks to see if a seat is already assigned
char Loop(char and);//Allows the user to repeat the program
void Show_output(char **airplane, int& rows, int& columns);//Displays the final seating arrangement

int main () {
	
	int rows;
	int columns;
	int seat;
	char letter;
	char** airplane;
	char ans;
	
	Get_input(rows, columns);
	airplane = IntSeating(rows,  columns);
	FillSeating(airplane,  rows,  columns);
	
	do {
		Assign_seat(airplane, seat, letter, rows, columns);
		Show_output(airplane, rows, columns);
		ans=Loop(ans);
	} while(ans == 'y' || 'Y');
	
	cout<<"End of Airplane Program";
	
    return 0;
}
/*
 Name: Get_Input
 Purpose: Gets the number of rows and columns for the array
 Arguments: int& rows(number of rows in the 2D array), int& columns (number of columns in the 2D array)
 */
void Get_input(int& rows, int& columns)
{
	cout<<"Airplane program! \n";
	cout<<"Enter the number of rows in the airplane: ";
	cin>>rows;
	cout<<"Enter the number of columns in the airplane: ";
	cin>>columns;
}
/*
 Name: IntSeating
 Purpose: Creates a 2D array
 Arguments: int& rows(number of rows in the 2D array), int& columns (number of columns in the 2D array)
 */
char** IntSeating(int& rows, int& columns)
{
	
	char** airplane = new char*[rows];
	
	for(int i=0; i < rows; i++)
		airplane[i] = new char[columns];
	
	return airplane;
}
/*
 Name: FillSeating
 Purpose: Fill the 2D array with letters
 Arguments: char **airplane(2D array of characters), int& rows(number of rows in the 2D array), int& columns (number of columns in the 2D array)
 */
void FillSeating(char **airplane, int& rows, int& columns) 
{
	for(int i=0; i < rows; i++)
	{
		for(int j=0; j < columns; j++)
		{
			airplane[i][j]='A'+j;
		}
	}
	Show_output(airplane, rows, columns);
	
}
/*
 Name: Assign_seat
 Purpose: Allows the user to pick a seat
 Arguments: char **airplane(2D array of characters), int seat (desired seat row), char& letter (desired seat column), int& rows(number of rows in the 2D array), int& columns (number of columns in the 2D array)
 */
void Assign_seat(char** airplane, int seat, char& letter, int& rows, int& columns)
{
	
	bool result;
	do{
		cout<<"\nEnter the seat row: ";
		cin>> seat;
		cout<<"\nEnter the seat letter: ";
		cin>> letter;
		if (seat > rows)
		{   
			cout<<"Invalid seat row. ";
			exit(1);
		} 
		
		seat=seat-1;
		
		Convert_letter(letter);
		if (letter > columns)
		{   
			cout<<"Invalid seat letter. ";
			exit(1);
		}
		
		result=Check_Seat(airplane, seat, letter);
		
	} while(result == false);
	
}
/*
 Name: Convert_letter
 Purpose: Converts a letter to a number
 Arguments: char& letter (desired seat column)
 */
void Convert_letter(char& letter)
{
	letter=letter-'A';
}
/*
 Name: Check_Seat
 Purpose: Checks to see if a seat has already been taken
 Arguments: char **airplane(2D array of characters), int seat(desired seat row), char& letter (desired seat column)
 */
bool Check_Seat(char **airplane, int seat, char& letter)
{
	if(airplane[seat][letter] == 'X') 
	{ 
		
		cout << "Seat is taken." << endl;
		return false;
	}
	else
	{
		airplane[seat][letter] = 'X';
		return true;
	}
	
}
/*
 Name: Loop
 Purpose: Repeats the program so the user can choose more seats
 Arguments: char ans (char Y or N)
 */
char Loop(char ans)
{
	cout<<"Would you like to choose another seat? (Y/N)?";
	cin>>ans;
	
	return ans;
}
/*
 Name: Show_output
 Purpose: Displays output of the 2D array to users
 Arguments: char **airplane(2D array of characters), int seat(desired seat row), char& letter (desired seat column)
 */
void Show_output(char **airplane, int& rows, int& columns)
{
	cout<<"\n Airplane Seating!\n\n";
	for(int i=0; i< rows; i++)
	{
		cout << i+1;
		for (int j=0; j< columns; j++)
			
			cout<<airplane[i][j];
		cout<<endl;
	}
}
/*
 Airplane program! 
 Enter the number of rows in the airplane: 7
 Enter the number of columns in the airplane: 7
 
 Airplane Seating!
 
 1ABCDEFG
 2ABCDEFG
 3ABCDEFG
 4ABCDEFG
 5ABCDEFG
 6ABCDEFG
 7ABCDEFG
 
 Enter the seat row: 4
 
 Enter the seat letter: B
 
 Airplane Seating!
 
 1ABCDEFG
 2ABCDEFG
 3ABCDEFG
 4AXCDEFG
 5ABCDEFG
 6ABCDEFG
 7ABCDEFG
 Would you like to choose another seat? (Y/N)?y
 
 Enter the seat row: 4
 
 Enter the seat letter: B
 Seat is taken.
 
 Enter the seat row: 5
 
 Enter the seat letter: F
 
 Airplane Seating!
 
 1ABCDEFG
 2ABCDEFG
 3ABCDEFG
 4AXCDEFG
 5ABCDEXG
 6ABCDEFG
 7ABCDEFG
 Would you like to choose another seat? (Y/N)?n
 
 */

